package org.belkodevelop.form;

import org.belkodevelop.announcement.domain.Announcement;
import org.belkodevelop.announcement.domain.OperationEnum;
import org.belkodevelop.geocoding.adress.json.GoogleAdress;
import org.belkodevelop.geocoding.adress.json.parser.GoogleAdressConverter;
import org.belkodevelop.geocoding.adress.json.parser.GoogleAdressParser;
import org.belkodevelop.geocoding.adress.json.parser.exception.GoogleAdressParserException;
import org.belkodevelop.realty.domen.*;
import org.belkodevelop.realty.factory.RealtyFactory;

public class NewRealtyForm {
    private String type;
    private String area;
    private Short numberRooms;
    private Short level;
    private Short levelsBuilding;
    private Double landArea;
    private String typeMetricLandArea;
    private String operation;
    private String description;
    private String adress;
    private Float price;
    
    private GoogleAdress googleAdress;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public Short getNumberRooms() {
        return numberRooms;
    }

    public void setNumberRooms(Short numberRooms) {
        this.numberRooms = numberRooms;
    }

    public Short getLevel() {
        return level;
    }

    public void setLevel(Short level) {
        this.level = level;
    }

    public Short getLevelsBuilding() {
        return levelsBuilding;
    }

    public void setLevelsBuilding(Short levelsBuilding) {
        this.levelsBuilding = levelsBuilding;
    }

    public Double getLandArea() {
        return landArea;
    }

    public void setLandArea(Double landArea) {
        this.landArea = landArea;
    }

    public String getTypeMetricLandArea() {
        return typeMetricLandArea;
    }

    public void setTypeMetricLandArea(String typeMetricLandArea) {
        this.typeMetricLandArea = typeMetricLandArea;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }
    
    public Realty getRealty() throws GoogleAdressParserException {
        Realty realty = RealtyFactory.getRealty(type);
        realty.setDescription(description);
        realty.setAdress(adress);
        
        realty.setGoogleAdressJSON(GoogleAdressParser.toJSON(googleAdress));
        realty.setCompositeAdress(GoogleAdressConverter.getCompositeAdress(getGoogleAdress()));
        
        if (realty instanceof Apartment) {
            Apartment apartment = (Apartment)realty;

            apartment.setArea(Double.valueOf(area));   
            apartment.setLevel(level);
            apartment.setLevelsBuilding(levelsBuilding);
            apartment.setNumberRooms(numberRooms);
            
        } else if (realty instanceof Garage) {
            Garage garage = (Garage)realty;
            
            garage.setArea(Double.valueOf(area));
        } else if (realty instanceof House) {
            House house = (House)realty;
            
            house.setArea(Double.valueOf(area));
            house.setLandArea(landArea);
            house.setNumberRooms(numberRooms);
            house.setTypeMetricLandArea(typeMetricLandArea);
        } else if (realty instanceof Land) {
            Land land = (Land)realty;
            land.setLandArea(landArea);
            land.setTypeMetricLandArea(typeMetricLandArea);
        } else if (realty instanceof Office) {
            Office office = (Office)realty;
            
            office.setArea(Double.valueOf(area));
            office.setLevel(level);
            office.setLevelsBuilding(levelsBuilding);
            office.setNumberRooms(numberRooms);
        }
        
        return realty;
    }
    
    public Announcement getAnnouncement() throws GoogleAdressParserException {
        Announcement announcement = new Announcement();
        announcement.setPrice(getPrice());
        announcement.setRealty(getRealty());
        announcement.setOperation(OperationEnum.valueOf(getOperation()));
        return announcement;
    }

    public GoogleAdress getGoogleAdress() {
        return googleAdress;
    }

    public void setGoogleAdress(GoogleAdress googleAdress) {
        this.googleAdress = googleAdress;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    @Override
    public String toString() {
        return String.format("NewRealtyForm{type=%s, area=%s, numberRooms=%s, level=%s, "
                + "levelsBuilding=%s, landArea=%s, typeMetricLandArea=%s, operation=%s,"
                + "description=%s, adress=%s, price=%s}", type, area, numberRooms, level,
                levelsBuilding, landArea, typeMetricLandArea, getOperation(), description,
                adress, price);
    }
}
